package com.hspedu.newData.dmsxl.贪心算法.mid.两个维度权衡;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-27 
 * @Version: 1.0
 */

public class LC135分发糖果 {

    // todo 还需要学习
    public int candy(int[] ratings) {

        int res = 0;
        int len = ratings.length;

        int[] left = new int[len];
        int[] right = new int[len];

        for (int i = 0; i < len; i++) {
            if (i > 0 && ratings[i] > ratings[i-1]){
                left[i] = left[i-1] + 1;
            }else {
                left[i] = 1;
            }
        }

        for (int i = len - 1; i >= 0; i--) {
            if (i < len - 1 && ratings[i] > ratings[i+1]){
                right[i] = right[i+1] + 1;
            }else {
                right[i] = 1;
            }
            res += Math.max(left[i],right[i]);
        }

        return res;
    }


    @Test
    public void test() {
        // 测试代码

        int candy = candy(new int[]{1, 2, 2});
        System.out.println(candy);

    }

}
